<?php

namespace app\admin\controller;
use app\model\FilesManage;
use app\util\ReturnCode;

class Index extends Base {
    public function upload() {
        $path = '/upload/' . date('Ymd', time()) . '/';
        $name = $_FILES['file']['name'];
        $tmp_name = $_FILES['file']['tmp_name'];
        $error = $_FILES['file']['error'];
        //过滤错误
        if ($error) {
            switch ($error) {
                case 1:
                    $error_message = '您上传的文件超过了PHP.INI配置文件中UPLOAD_MAX-FILESIZE的大小';
                    break;
                case 2:
                    $error_message = '您上传的文件超过了PHP.INI配置文件中的post_max_size的大小';
                    break;
                case 3:
                    $error_message = '文件只被部分上传';
                    break;
                case 4:
                    $error_message = '文件不能为空';
                    break;
                default:
                    $error_message = '未知错误';
            }
            die($error_message);
        }
        $arr_name = explode('.', $name);
        $hz = array_pop($arr_name);
        $new_name = md5(time() . uniqid()) . '.' . $hz;
        if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $path)) {
            mkdir($_SERVER['DOCUMENT_ROOT'] . $path, 0755, true);
        }
        if (move_uploaded_file($tmp_name, $_SERVER['DOCUMENT_ROOT'] . $path . $new_name)) {
            return $this->buildSuccess([
                'fileName' => $new_name,
                'fileUrl'  => $this->request->domain() . $path . $new_name
            ]);
           /* // 原来的注释掉
            // 曾 s
            // 获取图片属性
            $fileSrc = '.' . $path . $new_name;
//            $this ->debug('++++'.$_FILES['file']);
//            dump($fileSrc);
            $image = \think\Image::open($fileSrc);
            // 返回图片的宽度
            $width = $image->width() . 'px';
            // 返回图片的高度
            $height = $image->height() . 'px';
            // 返回图片的类型
            $type = $image->type();
            // 返回图片的mime类型
//            $mime = $image->mime();
            // 返回图片的大小
            $size = filesize($fileSrc);
            $kb = 1024;
            $mb = 1024 * $kb;
            $gb = 1024 * $mb;
            if ($size < $kb) {
                $size =  $size . "B";
            } elseif ($size < $mb) {
                $size = round($size / $kb, 2) . "KB";
            } elseif ($size < $gb) {
                $size = round($size / $mb, 2) . "MB";
            }
            // 保存到数据库 FilesManage
            $files = new FilesManage;
            // 模型对象赋值
            $files->data([
                'filename'  =>  $new_name,
                'url' =>  $path . $new_name,
                'imgwidth' => $width,
                'imgheight' => $height,
                'filetype' => $type,
                'filesize' => $size,
                'userid' => $this->request->post('userid'),
                'username' => $this->request->post('username'),
                'createTime' => date('Y-m-d H:i:s', time())
            ]);
            $files->save();
            $fileId = $files->id;
            // 获取新增数据的id
            return $this->buildSuccess([
                'fileId' => $fileId,
                'fileName' => $new_name,
//                'fileUrl'  => $this->request->domain() . $path . $new_name,
                'fileUrl'  => $path . $new_name,
                'width' => $width,
                'height' => $height,
                'type' => $type,
                'size' => $size,
            ]);
            // 曾 e*/
        } else {
            return $this->buildFailed(ReturnCode::FILE_SAVE_ERROR, '文件上传失败');
        }
    }
}
